<?php
require_once "google-api-php-client/examples/templates/base.php";
require_once "pfliegerAPI.php";
//include "safa.php"; //This function explains how to handle the returned Google Service Object
session_start();
set_include_path("google-api-php-client/src" . PATH_SEPARATOR . get_include_path());
require_once "Google/Client.php";
require_once "Google/Service/Calendar.php";

$BOOLFLAG = False;

/************************************************
ATTENTION: Fill in these values! Make sure
the redirect URI is to this page, e.g:
http://localhost:8080/user-example.php

EDIT: the redirect URI is very important.  failure to 
put the correct URI will result in an error 400 or 401 error. 
Make sure that the URI matches exactly the code declared in console
************************************************/
//'developer_key' => 'AIzaSyAVQdfNcTo-I5MOAMRb7p9Mkh1AgZ-OeNg'

$client_id = '318987637177-k2it649olrta3t6i8ao8o1fte10kq21f.apps.googleusercontent.com';
$client_secret = '7Wlx-ike9YnjYM5COY_qr2oT';
$redirect_uri ='http://web.engr.oregonstate.edu/~pflieged/NewCapStoneWeb/index.php';
$client = new Google_Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
//$client->setDveloperKey($api_key);
$client->setScopes(array("https://www.googleapis.com/auth/calendar","https://www.googleapis.com/auth/calendar.readonly"));








/************************************************
If we're logging out we just need to clear our
local access token in this case
************************************************/
if (isset($_REQUEST['logout'])) {
  unset($_SESSION['access_token']);
  echo 'you have logged out';
 
}



/************************************************
If we have a code back from the OAuth 2.0 flow,
we need to exchange that with the authenticate()
function. We store the resultant access token
bundle in the session, and redirect to ourself.
************************************************/
if (isset($_GET['code'])) {
  $client->authenticate($_GET['code']);
  $BOOLFLAG = True;
  $_SESSION['access_token'] = $client->getAccessToken();
  $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}

if ($BOOLFLAG == True)
	echo 'This is your time!';

/************************************************
If we have an access token, we can make
requests, else we generate an authentication URL.


EDIT: This is where the code starts once the user 
is authenticated.  
************************************************/

if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
  $client->setAccessToken($_SESSION['access_token']);

include 'NavBar.html'; //provides the navagation menu
include 'sidemenu.php';//provides the sidebar menu: given by bootstrap
include 'main_output_header.php';//holds container for main output



  
  
  
 } 
  
  
  

?>